package com.mdlib.helper.ui.fragments.rush.me;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ljlib.core.base.BaseFragment;
import com.ljlib.core.util.LogUtil;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.model.BaseResult;
import com.mdlib.helper.model.entity.RunInfo;
import com.mdlib.helper.model.entity.UserEntity;
import com.mdlib.helper.ui.UIHelper;
import com.mdlib.helper.ui.widget.springview.HelperHeads;
import com.mdlib.helper.ui.widget.springview.SpringView;
import com.mdlib.helper.utils.GlideCircleTransform;
import com.mdlib.helper.utils.ToastUtil;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 作者：HQY on 16/11/28 16:26
 * 邮箱：hqy_xz@126.com
 */

public class RushMeFragment extends BaseFragment {


    @Bind(R.id.tv_rush_me_nickname)
    TextView mTvRushMeNickname;
    @Bind(R.id.rb_rush_me_level)
    RatingBar mRbRushMeLevel;
    @Bind(R.id.ll_rush_me_name)
    LinearLayout mLlRushMeName;
    @Bind(R.id.tv_rush_me_balance)
    TextView mTvRushMeBalance;
    @Bind(R.id.ll_rush_me_balance)
    LinearLayout mLlRushMeBalance;
    @Bind(R.id.tv_rush_me_earnings)
    TextView mTvRushMeEarnings;
    @Bind(R.id.tv_me_today_order)
    TextView mTvMeTodayOrder;
    @Bind(R.id.tv_metoday_total_money)
    TextView mTvMetodayTotalMoney;
    @Bind(R.id.ll_rush_me_earnings)
    LinearLayout mLlRushMeEarnings;
    @Bind(R.id.tv_rush_me_achievement)
    TextView mTvRushMeAchievement;
    @Bind(R.id.tv_me_cumulative_success_order)
    TextView mTvMeCumulativeSuccessOrder;
    @Bind(R.id.tv_me_cumulative_earnings)
    TextView mTvMeCumulativeEarnings;
    @Bind(R.id.ll_rush_me_achievement)
    LinearLayout mLlRushMeAchievement;
    @Bind(R.id.iv_me_recommend_gift)
    ImageView mIvMeRecommendGift;
    @Bind(R.id.rl_me_recommend_gift)
    RelativeLayout mRlMeRecommendGift;
    @Bind(R.id.iv_me_alerts)
    ImageView mIvMeAlerts;
    @Bind(R.id.rl_me_alerts)
    RelativeLayout mRlMeAlerts;
    @Bind(R.id.iv_rush_me_problem)
    ImageView mIvRushMeProblem;
    @Bind(R.id.rl_rush_problem)
    RelativeLayout mRlRushProblem;
    @Bind(R.id.iv_rush_me_set)
    ImageView mIvRushMeSet;
    @Bind(R.id.rl_rush_me_set)
    RelativeLayout mRlRushMeSet;
    @Bind(R.id.iv_rush_me_head)
    ImageView mIvRushMeHead;
    @Bind(R.id.rl_me_voice_announcements)
    RelativeLayout mRlMeVoiceAnnouncement;
    @Bind(R.id.iv_me_voice_announcements)
    ImageView mIvVoiceAnnouncement;
    @Bind(R.id.sv_rush_me)
    SpringView mSvRushMe;

    public static RushMeFragment newInstance() {
        return new RushMeFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_rush_me;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
    }

    public void init() {
        mSvRushMe.setType(SpringView.Type.FOLLOW);
        mSvRushMe.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                getUserInfo();
                getRunInfo();
            }

            @Override
            public void onLoadmore() {

            }
        });
        mSvRushMe.setHeader(new HelperHeads(getActivity()));
        getUserInfo();
        getRunInfo();
    }

    @Override
    public void onStart() {
        super.onStart();
        init();
    }

    /**
     * 获取用户信息
     */
    public void getUserInfo() {
        startLoadingDialog(true);
        addSubscribe(DataManager.getInstance().getUserApi()
                .fetchUserInfo()
                .compose(RxHelper.<BaseResult<UserEntity>>rxSchedulerHelper())
                .subscribe(new RxSubscribe<BaseResult<UserEntity>>() {
                    @Override
                    protected void onSucc(BaseResult<UserEntity> userEntity) {
                        stopLoadingDialog();
                        UserEntity userInfo = userEntity.data;

                        mTvRushMeNickname.setText(userInfo.getNickName());
                        Glide.with(getActivity())
                                .load(userInfo.getAvatarUrl())
                                .placeholder(R.drawable.iv_app_default_head)
                                .error(R.drawable.iv_app_default_head)
                                .transform(new GlideCircleTransform(getActivity()))
                                .into(mIvRushMeHead);
                        mSvRushMe.onFinishFreshAndLoad();
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                        stopLoadingDialog();
                        mSvRushMe.onFinishFreshAndLoad();
                    }
                }));
    }


    /**
     * 跑客消息
     */
    private void getRunInfo() {
        addSubscribe(DataManager.getInstance().getUserApi()
                .getRunInfo()
                .compose(RxHelper.<RunInfo>defaultHandleResult())
                .subscribe(new RxSubscribe<RunInfo>() {
                    @Override
                    protected void onSucc(RunInfo runInfo) {
                        LogUtil.e(runInfo.toString());
                        mTvRushMeBalance.setText("￥" + runInfo.getBalance());
                        mTvMetodayTotalMoney.setText("￥" + runInfo.getMoney());
                        mTvMeTodayOrder.setText(runInfo.getOrder_num());
                        mTvMeCumulativeEarnings.setText("￥" + runInfo.getSum_money());
                        mTvMeCumulativeSuccessOrder.setText(runInfo.getSum_order_num());
                        mRbRushMeLevel.setRating(Float.parseFloat(runInfo.getPing_score()));
                        mSvRushMe.onFinishFreshAndLoad();
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                        mSvRushMe.onFinishFreshAndLoad();
                    }
                }));
    }

    @OnClick({R.id.ll_rush_me_balance, R.id.rl_me_recommend_gift, R.id.rl_me_alerts, R.id.rl_rush_problem, R.id.rl_rush_me_set, R.id.ll_rush_me_name , R.id.rl_me_voice_announcements})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_rush_me_name:
                UIHelper.showRushServiceStart(getActivity());
                break;
            case R.id.ll_rush_me_balance:
                UIHelper.showRushBalance(getActivity());
                break;
            case R.id.rl_me_recommend_gift:
                UIHelper.showRushShare(getActivity());
                break;
            case R.id.rl_me_alerts:
                UIHelper.showRushMessage(getActivity());
                break;
            case R.id.rl_rush_problem:
                UIHelper.showProblem(getActivity());
                break;
            case R.id.rl_rush_me_set:
                UIHelper.showSet(getActivity());
                break;
            case R.id.rl_me_voice_announcements:
                UIHelper.showVoice(getActivity());
                break;
        }
    }

}
